package com.uuah.server.config.spring.parsers.specific.endpoint.support;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

import com.uuah.server.config.spring.parsers.IUuahDefinitionParser;
import com.uuah.server.config.spring.parsers.PostProcessor;
import com.uuah.server.config.spring.parsers.assembly.IBeanAssembler;
import com.uuah.utils.StringUtils;

/**
 * Routines and constants common to the two endpoint definition parsers.
 * 
 * @see ChildEndpointDefinitionParser
 * @see OrphanEndpointDefinitionParser
 */
public class EndpointUtils {

	private static Logger logger = LoggerFactory.getLogger(EndpointUtils.class);
	public static final String CONNECTOR_ATTRIBUTE = "connector-ref";
	public static final String TRANSFORMERS_ATTRIBUTE = "transformer-refs";
	public static final String URI_BUILDER_ATTRIBUTE = "URIBuilder";
	public static final String ADDRESS_ATTRIBUTE = "address";

	private static void processTransformerDependencies(IBeanAssembler assembler,
			Element element) {
		if (StringUtils
				.isNotBlank(element.getAttribute(TRANSFORMERS_ATTRIBUTE))) {
			String[] trans = StringUtils.split(element
					.getAttribute(TRANSFORMERS_ATTRIBUTE), " ,;");
			for (int i = 0; i < trans.length; i++) {
				String ref = trans[i];
				if (logger.isDebugEnabled()) {
					logger.debug("transformer dep: " + ref);
				}
				assembler.getBean().addDependsOn(ref);
			}
		}
	}

	private static void processConnectorDependency(IBeanAssembler assembler,
			Element element) {
		if (StringUtils.isNotBlank(element.getAttribute(CONNECTOR_ATTRIBUTE))) {
			String ref = element.getAttribute(CONNECTOR_ATTRIBUTE);
			if (logger.isDebugEnabled()) {
				logger.debug("connector dep: " + ref);
			}
			assembler.getBean().addDependsOn(ref);
		}
	}

	public static void addPostProcess(IUuahDefinitionParser parser) {
		parser.registerPostProcessor(new PostProcessor() {
			public void postProcess(ParserContext unused,
					IBeanAssembler assembler, Element element) {
				EndpointUtils.processConnectorDependency(assembler, element);
				EndpointUtils
						.processTransformerDependencies(assembler, element);
			}
		});
	}

	public static void addProperties(IUuahDefinitionParser parser) {
		parser.addAlias(ADDRESS_ATTRIBUTE, URI_BUILDER_ATTRIBUTE);
		parser.addMapping("createConnector",
				"GET_OR_CREATE=0,ALWAYS_CREATE=1,NEVER_CREATE=2");
	}

}
